package com.landeb.shop.huo;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;

import com.landeb.shop.huo.bean.BeanUserData;
import com.google.gson.Gson;
import com.jkyeo.basicparamsinterceptor.BasicParamsInterceptor;
import com.liulishuo.filedownloader.FileDownloader;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.io.File;
import java.net.CookieManager;
import java.net.CookiePolicy;

import okhttp3.Cache;
import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by blade on 2016/11/14.
 */

public class MyApplication extends Application {

    public static final String BASE_URL = "http://www.landeb.com/index.php/Api/";
    public static final String BASE_DOMAIN = "http://www.landeb.com/";

    private static OkHttpClient mClient;
    //    private PersistentCookieStore mCookieStore;
//    private CookieJarImpl mCookieJar;
    public static boolean isCheckedUpdate=false;
    private static String token;
    private static String imei;
    public static String v = "1.0";
    private static  String os = "android";
    private SharedPreferences mPreferences;

    public static BeanUserData.DataBean mUserInfo;


    @Override
    public void onCreate() {
        super.onCreate();

        mPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        token = mPreferences.getString("token", "null");

        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        imei = telephonyManager.getDeviceId();

        String userData = mPreferences.getString("userData", null);
        if (userData != null) {
            Gson gson = new Gson();
            mUserInfo = gson.fromJson(userData, BeanUserData.DataBean.class);
        }

        createOkHttp();

        ZXingLibrary.initDisplayOpinion(this);


        FileDownloader.init(getApplicationContext());

    }

    private static void createOkHttp() {

        BasicParamsInterceptor mParamsInterceptor = new BasicParamsInterceptor.Builder()
                .addParam("token", token)
                .addParam("imei", imei)
                .addParam("v", v)
                .addParam("os", os)
                .build();

//        mCookieStore=new PersistentCookieStore(getApplicationContext());
//        mCookieJar=new CookieJarImpl(mCookieStore);

        CookieManager cookieManager = new CookieManager();
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
//        builder.cookieJar(new JavaNetCookieJar(cookieManager));

        mClient = new OkHttpClient.Builder()
                .addInterceptor(mParamsInterceptor)
//                .cookieJar(mCookieJar)
                .cookieJar(new JavaNetCookieJar(cookieManager))
                .connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();

    }

    public static void resetClient(String newToken) {

        token = newToken;
        createOkHttp();


    }

    public static OkHttpClient getClient() {
        return mClient;
    }

}
